package com.cskaoyan.mall.order.controller.chain;

import com.cskaoyan.mall.order.client.CartApiClient;
import com.cskaoyan.mall.order.query.OrderDetailParam;
import com.cskaoyan.mall.order.query.OrderInfoParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Pudding
 * @Date 2024/7/18 21:40
 * 3. 删除购物车中被选中的商品
 */
@Component
public class DeleteCartHandler extends AbstractHandler{

    @Autowired
    CartApiClient cartApiClient;

    @Override
    public void handle(HandlerContext handlerContext) {

        //获取责任链上下文对象参数
        OrderInfoParam orderInfoParam = handlerContext.getOrderInfoParam();
        String userId = handlerContext.getUserId();


        //3. 删除购物车中已经下单的商品
        //3.1 遍历订单中商品详细信息，将List<OrderDetailParam>转换为 List<商品id>
        List<Long> skuIds = orderInfoParam.getOrderDetailList().stream()
                .map(OrderDetailParam::getSkuId)
                .collect(Collectors.toList());
        //3.2 调用购物车中对外暴露的接口删除购物车中已经下单的商品
        cartApiClient.removeCartProductsInOrder(userId, skuIds);


        //4. 继续执行下一个责任链
        if (nextHandler != null){
            nextHandler.handle(handlerContext);
        }
    }
}
